import Cookie from 'js-cookie'
import { codeLogin } from '../../api/auth/auth'

//获取code
export const getQueryString = (name) => {

  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
  var context = "";
  if (r != null) context = r[2];
  reg = null;
  r = null;
  return context == null || context == "" || context == "undefined"
    ? ""
    : context;
}

//通过code登录获取token
export const authLogin = (code) => {
  codeLogin(code).then(res => {
    if (res.data.code != 200) {
      console.log('连接失败', res)
    } else {
      // Cookie.set('token', res.data.data)
      //将拿到的 Authorization 存入本地
      console.log('登录成功',res.data.data)
      localStorage.setItem('Authorization',res.data.data)
    }
  });
}

